<?php
namespace app\serveruser\traits;
use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-07-14 15:08:26
 * @Description: 运行已在service中验证完权限的那一条接口后，给该条信息或该列表信息添加用户信息。 仅请求一条接口
 */

trait RunOneAddUserInfo{

	//给一条结果添加用户详情
	protected function _runOneAddOneUserInfo($data, $path, $validate, $logic): array 
	{
		$sendData['query'] = json_encode([
            ["path"=>$path,"param"=>$data]
        ]);
        //获取path对应的信息
        $info = ApiBase::sendDataOne(config('proxy_queue_url'), $sendData);
        if($info['status'] != SUCCESS_STATUS){
        	return $info;
        }
        //给该条信息添加用户详情
        return $this->_complementUinfoOne($data, $info, $validate, $logic);
	}

	//给列表相对应的添加用户信息
	protected function _runOneAddLotsUserInfo($data, $path, $validate, $logic): array 
	{
		$sendData['query'] = json_encode([
            ["path"=>$path,"param"=>$data]
        ]);
        //获取path对应的信息
        $list = ApiBase::sendDataOne(config('proxy_queue_url'), $sendData);
        if($list['status'] != SUCCESS_STATUS){
        	return $list;
        }
        //给该条信息添加用户详情
        return $this->_complementUinfoList($data, $list, $validate, $logic);
	}

	//补全 "分页数据<直接return的分页数据结构>" 或 "列表数据<直接return的列表形式>" 的用户信息user_info
	protected function _complementUinfoList($data, $pageList, $validate, $logic): array 
	{
		//获取user_id
		isset($pageList['res']['data']) ? $dataList = $pageList['res']['data'] : $dataList = $pageList['res'];
		$userIdsString = implode(array_unique(get_field_to_array($dataList, 'user_id')), ","); //获取字符串user_ids

		//获取用户列表信息
		$userInfos = $this->_getUserList($data, $userIdsString, $validate, $logic);
		/*if($userInfos['status'] != SUCCESS_STATUS){
			return $userInfos;
		}*/

		//为每个元素对应的追加user_info
		$res = $this->_addUserInfo($dataList,$userInfos['res']); //补全数据列表的用户信息
		isset($pageList['res']['data']) ? $pageList['res']['data'] = $res : $pageList['res'] = $res;
		 
		return $pageList;

	}

	//补全详情状态的一维数组的用户信息user_info
	protected function _complementUinfoOne($data, $info, $validate, $logic): array 
	{

		$userInfos = $this->_getUserInfo($data, $info['res']['user_id'], $validate, $logic);
		if($userInfos['status'] != SUCCESS_STATUS){
			return $userInfos;
		}
		$info['res']['user_info'] = $userInfos['res'];
		return $info; 
	} 

	/*给数据添加user_info字段*/
	protected function _addUserInfo($dataList, $userList): array 
	{

		$userList = reset_array_key($userList, 'id');  //修改userList的键名的值为user_id
		foreach ($dataList as $k => &$v) {
			$v['user_info'] = isset($userList[$v['user_id']]) ? $userList[$v['user_id']] : [];
		}
		return $dataList;
	}

	//获取数据中的持有人的信息 
	protected function _getUserList($data, string $user_id, $validate, $logic): array 
	{
		$service = new ApiBase($data['token']);
		return $service->oneStepRun(['ids'=>$user_id],"serveruser/user/User/infoByIds",'', '1,0',$validate, $logic);
	}

	//获取单个用户信息
	protected function _getUserInfo(array $data, string $user_id, string $validate, string $logic): array 
	{
		$service = new ApiBase($data['token']);
		return $service->oneStepRun(['id'=>$user_id],"serveruser/user/User/info",'', '1,0', $validate, $logic);

	}
	
}